package com.cheeven.cwliu.citation.steps;

import java.util.HashMap;
import org.w3c.dom.Document;

public class Step2_2 extends StepBase {

	public static void refineSearch(HashMap<String, String> control) {
		logInfo("2.2. Refine Search Results by Document Types");

		/*
		 String sid = control.get("sid");
		 String qid = control.get("qid");
		 String documentType = control.get("documentType");

		 logInfo(1, "sid={0}, qid={1}, documentType={2}", sid, qid, documentType);

		 String urlString = "https://apps.webofknowledge.com/RAOnSummary.do?product=UA&search_mode=GeneralSearch"
		 + "&qid=" + qid
		 + "&SID=" + sid
		 + "&colName=&ra_name=DocumentType&action=expand";

		 String searchPage = httpGet(urlString);
		 Document domdoc = toDOM(searchPage);

		 String raMorePageUrl = "https://apps.webofknowledge.com/" + xpath("//a[@id='DocumentType']/@href", domdoc);

		 */

		/* 
		 * From the "Document Types" section in the navigation
		 * Click the "more options / values..." link to open the 
		 * Refine the "Document Types" page
		 */
		String documentType = control.get("documentType");
		String raMorePageUrl = control.get("raMorePageUrl");
		logInfo(1, "documentType={0}, raMorePageUrl={1}", documentType, raMorePageUrl);
		String raMorePage = httpGet(raMorePageUrl);
		Document domdoc = toDOM(raMorePage);

		control.put("sid", xpath("//input[@name='SID']/@value", domdoc));
		control.put("qid", xpath("//input[@name='qid']/@value", domdoc));
		control.put("parentQid", xpath("//input[@name='parentQid']/@value", domdoc));

		logInfo(1, "sid={0}, parentQid={1}, documentType={2}", control.get("sid"), control.get("parentQid"), documentType);

		/* 
		 * Refine the search result for the document type
		 */
		logInfo(1, "httpPost={0}", "https://apps.webofknowledge.com/Refine.do");
		String refinedSearchPage = httpPost(
				"https://apps.webofknowledge.com/Refine.do",
				new String[][]{
					{"exclude", ""},
					{"more_sort_order", "default"},
					{"x", "47"},
					{"y", "9"},
					{"refineSelection", documentType},
					{"parentQid", control.get("parentQid")},
					{"SID", control.get("sid")},
					{"product", "UA"},
					{"databaseId", "FASTRDB"},
					{"search_mode", "Refine"},
					{"queryOption(summary_search_mode)", "GeneralSearch"},
					{"action", "search"},
					{"clickRaMore", "Your refine selection on the screen will not be remembered if you continue with 'more ...' feature."},
					{"openCheckboxes", "Your refine selection in this left-panel will not be remembered if you hide it."},
					{"refineSelectAtLeastOneCheckbox", "Please select at least one checkbox to refine your results."},
					{"queryOption(sortBy)", "PY.D;LD.D;SO.A;VL.D;PG.A;AU.A"},
					{"queryOption(ss_query_language)", "auto"},
					{"sws", ""},
					{"swsFields", "TS"},
					{"swsHidden", "Search within the first 100,000<br>results for"},
					{"exclude", ""},
					{"exclude", ""}
				});
		domdoc = toDOM(refinedSearchPage);

		control.put("sid", xpath("//input[@name='SID']/@value", domdoc));
		control.put("qid", xpath("//input[@name='qid']/@value", domdoc));
		control.put("exitRefineSearchUrl", xpath("//div[@class='NEWbackLink']/a/@href", domdoc));

		logInfo(1, "sid={0}, qid={1}, exitRefineSearchUrl={2}", control.get("sid"), control.get("qid"), control.get("exitRefineSearchUrl"));

	}
}
